﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using OverStore.Runtime.OverStoreContext;
using OverStore.Exceptions.Configuration;
using OverStore.Runtime.ObjectRepository.ErrorTransformer;

namespace OverStore.Configuration.ObjectRepository.ErrorTransformer
{
    /// <summary>
    /// Contains configuration parameters allows to create instances of <see cref="MatchErrorTransformer{TInstance}"/>.
    /// </summary>
    public class MatchErrorTransformerConfiguration<TInstance> : IComponentConfiguration<IErrorTransformer<TInstance>>
        where TInstance : class
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MatchErrorTransformerConfiguration&lt;TInstance&gt;"/> class.
        /// </summary>
        public MatchErrorTransformerConfiguration()
        {
            MatchCases = new List<MatchCase>();
        }

        /// <summary>
        /// Gets or sets the collection of the match cases.
        /// </summary>
        public List<MatchCase> MatchCases { get; set; }

        /// <summary>
        /// Creates the new instance of <typeparamref name="TComponent"/> type using set of services provided by <paramref name="context"/> object.
        /// </summary>
        /// <param name="context">The context provides access to services shared between components.</param>
        /// <returns>
        /// The new instance of <typeparamref name="TComponent"/> class.
        /// </returns>
        public IErrorTransformer<TInstance> CreateComponent(IOverStoreContext context)
        {
            if (MatchCases == null || MatchCases.Count == 0)
                throw this.ConfigurationException("Match Cases are missing.");
            return new MatchErrorTransformer<TInstance>(MatchCases);
        }
    }
}
